<?php

namespace app\modules\clinical\models;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "excel_pd".
 *
 * @property integer $pk_id
 * @property string $ft_ill_time
 * @property string $ft_symptom
 * @property string $ft_part
 * @property string $ft_process
 * @property string $ft_md_time
 * @property string $up_time
 * @property string $bf_time
 * @property string $lc_time
 * @property string $dn_time
 * @property string $sl_time
 * @property string $med
 * @property string $else_ctnt
 * @property string classify
 * @property string $treatment
 */
class ExcelPd extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'excel_pd';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ft_ill_time', 'ft_md_time', 'up_time', 'bf_time', 'lc_time', 'dn_time', 'sl_time', 'treatment'], 'required'],
            [['ft_ill_time', 'ft_md_time', 'up_time', 'bf_time', 'lc_time', 'dn_time', 'sl_time', 'med'], 'safe'],
            [['ft_symptom', 'ft_process', 'else_ctnt', 'classify'], 'string', 'max' => 255],
            [['treatment', 'q_1','q_2','q_3','q_4','q_5','q_6','q_7','q_8','q_9','q_10','q_11','q_12','q_13','q_14','q_15','q_16','q_17','q_18','q_19','q_20','q_21','q_22','q_23','q_24','q_25','q_26','q_27','q_28','q_29','q_30'], 'integer'],
            [['ft_part'], 'filter', 'filter' => function($array) {
                return is_array($array) ? implode("+", $array) : "";
            }],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ft_ill_time' => '首次起病时间',
            'ft_symptom' => '首次起病症状',
            'ft_part' => '首次起病部位',
            'ft_process' => '病情进展过程',
            'ft_md_time' => '首次用药时间',
            'up_time' => '起床时间',
            'bf_time' => '早餐时间',
            'lc_time' => '午餐时间',
            'dn_time' => '晚餐时间',
            'sl_time' => '睡觉时间',
            'else_ctnt' => '其他用药',
            'treatment' => '手术治疗',
            'classify' => 'H-Y分级',
            'q_1' => '1. 白天流口水',
            'q_2' => '2. 味觉或嗅觉功能减退',
            'q_3' => '3. 吞咽困难或呛咳',
            'q_4' => '4. 恶心或呕吐',
            'q_5' => '5. 便秘（一周少于三次排便）或排大便困难',
            'q_6' => '6. 大便失禁',
            'q_7' => '7. 排便后有未排净的感觉',
            'q_8' => '8. 尿急',
            'q_9' => '9. 夜间较规律的起床排尿',
            'q_10' => '10. 不能解释的疼痛（排除由其他疾病引起的痛，如关节炎）',
            'q_11' => '11. 不能解释的体重变化（体重减轻不是因为改变饮食）',
            'q_12' => '12. 对近期发生的事情记忆有困难，或忘记做一些事',
            'q_13' => '13. 对周围发生的事情失去兴趣，或者没有兴趣做事情',
            'q_14' => '14. 看到或听到周围并不存在的东西',
            'q_15' => '15. 难以集中精神',
            'q_16' => '16. 感觉悲哀、情绪低落',
            'q_17' => '17. 感觉焦虑、恐惧、恐慌',
            'q_18' => '18. 性欲减退或性欲增强',
            'q_19' => '19. 性生活困难',
            'q_20' => '20. 从卧位或坐位站立时觉得头晕、头轻或乏力',
            'q_21' => '21. 摔倒',
            'q_22' => '22. 常在一些场合打盹，如开会、开车、吃饭时',
            'q_23' => '23. 夜间入睡困难或容易醒',
            'q_24' => '24. 做梦多，梦境很逼真，或经常做噩梦',
            'q_25' => '25. 睡梦时说梦话或舞动肢体，好像在扮演梦中的角色',
            'q_26' => '26. 夜间或休息时双下肢不适感，需不断活动才能缓解症状',
            'q_27' => '27. 腿部水肿',
            'q_28' => '28. 多汗',
            'q_29' => '29. 复视，看东西重影',
            'q_30' => '30. 总觉得一些事情要发生，尽管这些担心并不存在',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            $this->ft_ill_time = strtotime($this->ft_ill_time);
            $this->ft_md_time = strtotime($this->ft_md_time);
            return true;

        } else{
            return false;
        }
    }

    public function load($data, $formName = null)
    {
        parent::load($data, $formName);
        if (isset($data['MedCtn'])) {
            $medCtn = $data['MedCtn'];
            $med_array = array();
            foreach ($medCtn as $key => $value) {

                if ($value == 0 || $value == '') continue;

                $add = new \stdClass();
                $add->id = $key;
                $add->value = floatval($value);
                $med_array[] = $add;
            }

            $this->med = json_encode($med_array);
            return true;
        }

        return false;
    }

    public function defaultValue()
    {
        $this->ft_ill_time = strtotime(date("Y-m"));
        $this->ft_md_time = strtotime(date("Y-m"));
        $this->up_time = "06:00";
        $this->bf_time = "07:00";
        $this->lc_time = "11:00";
        $this->dn_time = "18:00";
        $this->sl_time = "22:00";
        $this->treatment = 2;

        for ($i = 1; $i <= 30; $i ++) {
            $q = "q_".$i;
            $this->$q = 0;
        }
    }

    public function getCrtinfo()
    {
        return $this->hasOne(Crtinfo::className(), ['excel_id' => 'pk_id']);
    }

    public function getString($data, $type) {
        if ($data == '') return '';
        
        $elements = explode("+", $data);
        $string = "";
        $params = $this->getParams($type);

        foreach ($elements as $key) {

            $string .= $params[intval($key)] . ", ";
        }

        $string = substr($string, 0, strlen($string)-2);

        return $string;
    }

    public function getMedCtns()
    {
        $result = array();
        if ($this->med != '') {
            $json = $this->med;
            $json = json_decode($this->med);
            foreach ($json as $med) {
                $id = explode('_', $med->id);
                $result[$id[0]][$id[1]] = $med->value;
            }
        }
        return $result;
    }

    static public function getMedList($type) {
        $result = (new Query())
            ->select(['pk_id', 'med_name'])
            ->from('med_list')
            ->where(['type' => strtoupper($type)])
            ->all();

        return $result;
    }

    //clear data before the form view
    public function clear()
    {
        // ft_part
        if ($this->ft_part != '' && !is_array($this->ft_part)) {
            $this->ft_part = explode('+', $this->ft_part);
        }

        // ft_ill_time & ft_md_time
        if (is_string($this->ft_ill_time)) {
            $this->ft_ill_time = strtotime($this->ft_ill_time);
        }
        if (is_string($this->ft_md_time)) {
            $this->ft_md_time = strtotime($this->ft_md_time);
        }
    }

    public static function getParams($type = false, $code = false) {
        $params = [
            'disease' => array('PD', 'ET', 'FCMTE', 'SCA', 'HSP', 'PFBC', 'CMT', '其他'),

            'illPart' => array('0' => '左下肢', '1' => '左上肢', '2' => '右下肢', '3' => '右上肢'),

            'operation' => array('0' => '脑起博器(DBS)', '1' => '苍白球损毁术', '2' => '无'),

            'yesno' => array('1' => '是', '0' => '否'),
            'youwu' => array('1' => '有', '0' => '无'),
        ];

        if ($code !== false) {
            return ($type) ? $params[$type][$code] : '';
        }

        return ($type) ? $params[$type] : $params;
    }
}
